package com.pctx.bibawxminiprogram.controller;

import com.pctx.bibacommon.result.Result;
import com.pctx.bibawxminiprogram.service.WxApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 小程序的登录
 * Created by wnf on 2018/3/10.
 */
@RequestMapping("/wx")
@RestController
public class WxApiController {

    private static final Logger LOGGER = LoggerFactory.getLogger(WxApiController.class);

    @Autowired
    private WxApiService wxApiService;

    /**
     * 小程序获取openId
     * @param js_code
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getOpenId")
    public Map<String, Object> getOpenId(String js_code)throws Exception{
        return wxApiService.getOpenId(js_code);
    }

    /**
     * 通过code来获取openId和session_key
     * @param code
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/login")
    public Result login(@RequestParam(value = "code",required = true) String code)throws Exception{
        System.out.println(code);
        return wxApiService.jscode2session(code);
    }

    /**
     * 检查登录态
     * @param localSessionKey
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/checkSession")
    public Result checkSession(@RequestParam(value = "localSessionKey",required = true) String localSessionKey)throws Exception{
        return wxApiService.checkSession(localSessionKey);
    }

    /**
     * 更新用户信息
     * @param userUuid
     * @param localSessionKey
     * @param nickName
     * @param avatarUrl
     * @param gender
     * @param province
     * @param city
     * @param country
     * @param rawData
     * @param signature
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateUserInfo")
    public Result updateUserInfo(@RequestParam(value = "userUuid",required = true) String userUuid,@RequestParam(value = "localSessionKey",required = true) String localSessionKey,
                                 @RequestParam(value = "nickName",required = false) String nickName, @RequestParam(value = "avatarUrl",required = false) String avatarUrl,
                                 @RequestParam(value = "gender",required = false) String gender, @RequestParam(value = "province",required = false) String province,
                                 @RequestParam(value = "city",required = false) String city, @RequestParam(value = "country",required = false) String country,
                                 @RequestParam(value = "rawData",required = true) String rawData, @RequestParam(value = "signature",required = true) String signature)throws Exception{

        return wxApiService.updateUserInfo(userUuid,localSessionKey,nickName,avatarUrl,gender,province,city,country,rawData,signature);
    }

    /**
     * 绑定手机号码到指定的用户
     * @param userUuid
     * @param Mobile
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/bindMobileToUser")
    public Result bindMobileToUser(@RequestParam(value = "userUuid",required = true) String userUuid,
                                   @RequestParam(value = "mobile",required = true) String Mobile) throws  Exception{
        return wxApiService.bindMobileToUser(userUuid,Mobile);
    }

    /**
     * 获取解密后的用户信息
     * @param encryptedData
     * @param localSessionKey
     * @param iv
     * @return
     */
    /*@RequestMapping(value = "/getUserInfo")
    public Result getUserInfo(String encryptedData, String localSessionKey, String iv) throws Exception{
        return wxApiService.getUserInfo(encryptedData,localSessionKey,iv);
    }*/

    /**
     * 通过手机号码来获取验证码返回给前端
     * @param mobile
     * @return
     * @throws Exception
     */
    /*@RequestMapping(value = "/getCaptchaByMobile")
    public Result getCaptchaByMobile(String mobile) throws Exception{
        return wxApiService.getCaptchaByMobile(mobile);
    }*/

    /**
     * 绑定手机号
     * @param openid
     * @param mobile
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/bindMobile")
    public Result bindMobile(@RequestParam(value = "openid",required = true) String openid,
                             @RequestParam(value = "mobile",required = true) String mobile)throws Exception{
        return wxApiService.bindMobile(openid,mobile);
    }

    /**
     * 根据openId来取用户信息
     * @param openid
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getUserInfoByOpenid")
    public Result getUserInfoByOpenid(@RequestParam(value = "openid",required = true) String openid)throws Exception{
        return wxApiService.getUserInfoByOpenid(openid);
    }
}
